package org.swust.sysmonitorapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.swust.sysmonitorapp.entity.DeviceStatus;
import org.swust.sysmonitorapp.mapper.DeviceStatusMapper;
import org.swust.sysmonitorapp.service.IDeviceStatusService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备状态服务实现类
 *
 * @since 2024-03-21
 */
@Service
public class DeviceStatusServiceImpl extends ServiceImpl<DeviceStatusMapper, DeviceStatus> implements IDeviceStatusService {

    @Override
    public boolean updateDeviceStatus(String deviceId, String status) {
        DeviceStatus deviceStatus = getById(deviceId);
        LocalDateTime now = LocalDateTime.now();

        if (deviceStatus == null) {
            // 新增设备状态记录
            deviceStatus = new DeviceStatus();
            deviceStatus.setDeviceId(deviceId);
            deviceStatus.setStatus(status);
            deviceStatus.setLastOnlineTime(now);
            return save(deviceStatus);
        } else {
            // 使用Mapper中的方法更新设备状态
            return baseMapper.updateDeviceStatus(deviceId, status, now) > 0;
        }
    }

    @Override
    public List<DeviceStatus> getAllDeviceStatus() {
        return baseMapper.getAllDeviceStatus();
    }
}